package com.itchen.controller.module;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itchen.annotation.Log;
import com.itchen.common.PageResult;
import com.itchen.common.Result;
import com.itchen.domain.entity.Role;
import com.itchen.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 角色 Controller
 */
@Tag(name = "角色管理", description = "角色相关接口")
@RestController
@RequestMapping("/role")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("getAll")
    @Operation(summary = "获取所有角色信息")
    @Log(operationType = "查询", operationModule = "角色管理")
    public Result<List<Role>> getAllRoles() {
        List<Role> roles = roleService.list();
        return Result.success(roles);
    }

    @GetMapping("getByPage")
    @Operation(summary = "根据Page获取角色信息")
    @Log(operationType = "查询", operationModule = "角色管理")
    public Result<PageResult<Role>> getAllRolesByPage(Long pageNum, Long pageSize, String name) {
        // 创建查询条件
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();

        // 动态添加查询条件
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }

        Page<Role> page = roleService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    @GetMapping("getById/{id}")
    @Operation(summary = "根据ID获取角色")
    @Log(operationType = "查询", operationModule = "角色管理")
    public Result<Role> getRoleById(@PathVariable Integer id) {
        return Result.success(roleService.getById(id));
    }

    @PostMapping("add")
    @Operation(summary = "添加角色")
    @Log(operationType = "新增", operationModule = "角色管理")
    public Result<Boolean> addRole(@RequestBody Role role) {
        return Result.success(roleService.save(role));
    }

    @PutMapping("update")
    @Operation(summary = "更新角色")
    @Log(operationType = "修改", operationModule = "角色管理")
    public Result<Boolean> updateRole(@RequestBody Role role) {
        return Result.success(roleService.updateById(role));
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除角色")
    @Log(operationType = "删除", operationModule = "角色管理")
    public Result<Boolean> deleteRole(@PathVariable Integer id) {
        return Result.success(roleService.removeById(id));
    }
} 